<?php

namespace app\admin\model\manufacture;

use think\Model;

class Maintenance extends Model
{
    // 表名
    protected $name = 'manufacture_maintenance';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    
    // 追加属性
    protected $append = [
        'maintenance_type_text'
    ];
    
    // 维护类型
    public function getMaintenanceTypeList()
    {
        return [
            'routine' => __('例行维护'),
            'repair' => __('故障维修'),
            'inspection' => __('设备检查'),
            'status_change' => __('状态变更')
        ];
    }

    public function getMaintenanceTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['maintenance_type'];
        $list = $this->getMaintenanceTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    // 关联设备
    public function equipment()
    {
        return $this->belongsTo('Equipment', 'equipment_id', 'id');
    }

    // 关联操作员
    public function operator()
    {
        return $this->belongsTo('app\admin\model\Admin', 'operator_id', 'id');
    }
}
